(function () {
    var io = ppt.io;

    var RoleUI = (function (_super) {
        var CAMP = {
            "red" : 0,
            "blue" : 1
        };

        function RoleUI(data, index) {
            RoleUI.super(this);

            this.index = null;

            this.init(data, index);
        }
        Laya.class(RoleUI, "", _super);
        var _proto = RoleUI.prototype;
        _proto.destroy = function () {
            _super.prototype.destroy.call(this, true);

            this.camp = null;
            this.avatart = null;
            this.userName = null;
            this.index = null;
        }

        _proto.init = function (data, index) {
            this.index = index;
            this.setup(index);
            this.update(data);
        }
        _proto.setup = function (index) {
            this.pos(-216, index * 80);

            var mask = new Laya.Sprite();
                mask.loadImage("res/public/avatar_mask.png", 0, 0, this.avatar.width, this.avatar.height);
            this.avatar.mask = mask;
        }
        _proto.update = function (data) {
            this.camp.index = CAMP[data.camp];
            this.userName.text = data.userName;
            this.avatar.skin = data.avatar != "" ? data.avatar : USER_DEFAULT_AVATAR;
        }

        _proto.enter = function () {
            Laya.Tween.to(this, {x : 0}, 500, Laya.Ease.backOut, Laya.Handler.create(this, function () {
                this.loopAni.play();
            }), this.index * 500 + 200);
        }

        return RoleUI;
    })(ui.Game.UI.RoleInfoUI);


    (function (_super) {
        var ACTIONS = {
            "role::die" : function (data) {
                this.updateStatus("die", data);
            },
            "role::release" : function (data) {
                this.updateStatus("release", data);
            },
            "role::reborn" : function (data) {
                this.updateStatus("release", [data.userId]);
            }
        };

        function RoleInfo(data) {
            RoleInfo.super(this);

            this.rolesUI = {};

            this.init(data);
        }
        Laya.class(RoleInfo, "Game.RoleInfo", _super);
        var _proto = RoleInfo.prototype;
        _proto.destroy = function () {
            _super.prototype.destroy.call(this, true);

            io.unregister(ACTIONS);
            
            this.rolesUI = null;
        }

        _proto.init = function (rolesData) {
            this.pos(0, 25);
            io.register(ACTIONS, this);

            this.create(rolesData);
        }
        _proto.create = function (rolesData) {
            for(var i in rolesData){
                var roleInfo = new RoleUI(rolesData[i], i);
                    roleInfo.y = (i * 80);
                this.addChild(roleInfo);

                this.rolesUI[rolesData[i].userId] = roleInfo;
            }
        }
        _proto.updateStatus = function (act, userIds) {
            switch(act){
                case "release":
                    for(var i in userIds){
                        this.rolesUI[userIds[i]].disabled = false;
                    }
                    break;
                case "die":
                    for(var i in userIds){
                        this.rolesUI[userIds[i]].disabled = true;
                    }
                    break;
            }
        }

        _proto.enter = function () {
            var index = 0;
            for(var i in this.rolesUI){
                this.rolesUI[i].enter();
            }
        }
    })(Laya.Sprite);
})();